package controllers.manage.complaint;

import bill.Bill;
import controllers.BaseController;
import enums.BillOutStatus;
import enums.ReportUserType;
import enums.TrialType;
import helper.GlobalConfig;
import jodd.util.StringUtil;
import models.merchant.Merchant;
import models.msgtemp.admin.WxMpAdminMsgTemp;
import models.weixin.WebUser;
import order.OrderItem;
import order.Report;
import order.ReportRemark;
import play.Logger;
import play.mvc.With;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/8.
 */
@With(BaseController.class)
public class ComplaintCtrl extends BaseController {

    public static void  index(Integer pageNumber, String orderNumber, String merchantName,String userName){
        pageNumber = pageNumber == null ? 1 : pageNumber;

        Map<String , Object> searchMap = new HashMap<>();

        if(StringUtil.isNotBlank(merchantName)) {
            searchMap.put("merchantName", "%"+merchantName+"%");
        }
        if(StringUtil.isNotBlank(orderNumber)) {
            searchMap.put("orderNumber", orderNumber);
        }
        if(StringUtil.isNotBlank(userName)) {
            searchMap.put("userName", "%"+userName+"%");
        }
        List<Report> reportList = Report.findByConditionList(searchMap,"createAt desc",pageNumber, GlobalConfig.PAGE_SIZE);
        flash.put("menuId", "29");
        render(reportList,pageNumber,merchantName,orderNumber,userName);
    }
    /**
     * 投诉查询详细信息
     */
    public static void detail(long id, Integer pageNumber){
        Report report = Report.findById(id);
        List<ReportRemark> reportRemarks = null;
        if(report != null){
            reportRemarks = ReportRemark.getBydReport(report);
        }

        render(report,reportRemarks, pageNumber);
    }
    /**
     * 投诉
     */
    public static void trialConfirm(ReportUserType userType, long  reportId){
        Logger.info("reportId=================== : %s" , reportId);
        Map<String , Object> resultMap = new HashMap<>();
        if(reportId > 0){
            Report report =  Report.findById(reportId);
            if(report == null){
                resultMap.put("success",false);
                resultMap.put("msg","投诉单不存在");
                renderJSON(resultMap);
            }
            WebUser webUser = report.orderItem.order.webUser ;
            if(webUser == null) {
                resultMap.put("success",false);
                resultMap.put("msg","用户信息不存在,操作失败!");
                renderJSON(resultMap);
            }
            Merchant merchant = report.merchant;
            if(merchant == null ){
                resultMap.put("success",false);
                resultMap.put("msg","商户信息不存在,操作失败!");
                renderJSON(resultMap);
            }
            OrderItem orderItem  = OrderItem.findById(report.orderItem.id);
            if(orderItem == null){
                resultMap.put("success",false);
                resultMap.put("msg","订单明细不存在");
                renderJSON(resultMap);
            }
            if(userType == ReportUserType.MERCHANT){
                report.trial = TrialType.MERCHANT;
            }else{
                report.trial = TrialType.USER;
            }
            report.save();

            //如果商家投诉成功 。 冻结资金返还
            if(report.trial == TrialType.MERCHANT) {
//                Merchant merchant = report.merchant;
                merchant.frozenAmount = merchant.frozenAmount == null ? 0.0d : merchant.frozenAmount;
                merchant.frozenAmount -= report.amount;
                merchant.withdrawalsAmount = merchant.withdrawalsAmount == null ? 0.0d : merchant.withdrawalsAmount;
                merchant.withdrawalsAmount += report.amount;
                merchant.save();
                Bill bill = Bill.getBillByOrderItem(orderItem);
                if(bill != null){
                    bill.outStatus = BillOutStatus.NORMAL;
                    bill.save();
                }
            }

            //如果用户投诉成功
            if(report.trial == TrialType.USER) {
//                Merchant merchant = report.merchant;
                merchant.frozenAmount = merchant.frozenAmount == null ? 0.0d : merchant.frozenAmount;
                merchant.frozenAmount -= report.amount;
                merchant.complaintCount = merchant.complaintCount != null ? merchant.complaintCount+1 : 1;
                merchant.save();
//               WebUser  webUser = report.orderItem.order.webUser;
                webUser.withdrawalsAmount = webUser.withdrawalsAmount == null? 0.0d : webUser.withdrawalsAmount;
                webUser.withdrawalsAmount += report.amount;
                webUser.save();
//                WxMsgTempUtil.sendMsgforReportMerchant(merchant);
            }

            //发送至客户
            WxMpAdminMsgTemp.sendMsgforReportToUser(webUser , report);
            //发送至商家
            WebUser merchantUser = WebUser.findByUserId(merchant.webUserId);
            ReportRemark reportRemark = ReportRemark.getByMerchantAndReport(new Merchant() , report);
            WxMpAdminMsgTemp.sendMsgforReportToMerchant(merchantUser , report,reportRemark);

        }else{
            resultMap.put("success",false);
            resultMap.put("msg","投诉单不存在");
            renderJSON(resultMap);
        }
        resultMap.put("success",true);
        resultMap.put("msg","审判结束");
        renderJSON(resultMap);

    }



}
